package co.gov.medellin.sdep.sisdep.model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.object.MappingSqlQuery;
import org.springframework.jdbc.object.SqlUpdate;

import co.gov.medellin.sdep.sisdep.model.FeriaCritValoracion;

public class FeriaCritValoracionDAOImpl extends JdbcDaoSupport implements IFeriaCritValoracionDAO {

    private InsertOperation insertOperation;

    private UpdateOperation updateOperation;

    private DeleteOperation deleteOperation;

    private DeleteByPrimaryKey deleteByPrimaryKey;

    private FindByPrimaryKeyQuery findByPrimaryKeyQuery;

    private FindByCriteriaQuery findByCriteriaQuery;

    private FindAllQuery findAllQuery;
    
    private FindFeriaId findFeriaId;
    
    private CargarCriterios cargarCriterios;

    private FindByActiveStateQuery findByActiveStateQuery;

	private static final String SELECT = "SELECT * FROM REG_FERIA_CRIT_VALORACION_T ";
	private static final String WHERE = " WHERE ";
	private static final String PARAMETER = " ?, ";

    public void delete(FeriaCritValoracion feriaCritValoracion) {
		deleteOperation.execute( feriaCritValoracion  );
	}

	public void deleteByPrimaryKey(long id) {
		deleteByPrimaryKey.execute(id);
	}

	public FeriaCritValoracion findByPrimaryKey(long login) {
		return findByPrimaryKeyQuery.runQuery(login);
	}

	public void insert(FeriaCritValoracion feriaCritValoracion) {
		insertOperation.execute( feriaCritValoracion );
	}

	public void update(FeriaCritValoracion feriaCritValoracion) {
		updateOperation.execute( feriaCritValoracion );
	}

	public FeriaCritValoracion findByCriteria(FeriaCritValoracion  feriaCritValoracion) {
		return findByCriteriaQuery.runQuery(feriaCritValoracion);
	}

	public List load() {
		return findAllQuery.runQuery();
	}
	
	public List cargarPorFeria(FeriaCritValoracion  feriaCritValoracion) {
		return findFeriaId.runQuery(feriaCritValoracion);
	}
	
	public List cargaCriterios() {
		return cargarCriterios.runQuery();
	}

	public FeriaCritValoracion findByActiveState(FeriaCritValoracion  feriaCritValoracion) {
		return findByActiveStateQuery.runQuery(feriaCritValoracion);
	}

	public void initDao() {
    	insertOperation   = new InsertOperation( getDataSource() );
    	updateOperation = new UpdateOperation(  getDataSource() );
    	//deleteOperation = new DeleteOperation(  getDataSource() );
    	//deleteByPrimaryKey = new DeleteByPrimaryKey( getDataSource());
    	//findByPrimaryKeyQuery   = new FindByPrimaryKeyQuery(  getDataSource() );
    	findByCriteriaQuery = new FindByCriteriaQuery(  getDataSource() );
    	//findAllQuery = new FindAllQuery( getDataSource() );
    	findFeriaId = new FindFeriaId( getDataSource() );
    	//findByActiveStateQuery = new FindByActiveStateQuery( getDataSource() );
    	//cargarCriterios = new CargarCriterios( getDataSource());
	}
	

    // ------------- SaveOperation ---------------------------------------
	class InsertOperation extends SqlUpdate	{
		private static final String sql = "INSERT INTO REG_FERIA_CRIT_VALORACION_T ("
				  + " ID, "
				  + " ID_CRITERIO, "
				  + " ID_FERIA_ARTESANAL, "
				  + " CODIGO, "
				  + " VALORACION, "
				  + " OBSERVACION, "
				  + " ESTADO, "
				  + " USUARIO_CREA, "
				  + " FECHA_CREA "
				  + ") VALUES ("
			  	  + " REG_FERIA_CRIT_VALORACION_SEC.nextVal, "
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + "SYSDATE"
				  + ")";
		public  InsertOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters			
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			compile();
		}
		public void execute(FeriaCritValoracion feriaCritValoracion) {
			InsertOperation.this.update( new Object[]{
				feriaCritValoracion.getIdCriterio(),
				feriaCritValoracion.getIdFeriaArtesanal(),
				feriaCritValoracion.getCodigo(),
				feriaCritValoracion.getValoracion(),
				feriaCritValoracion.getObservacion(),
				feriaCritValoracion.getEstado(),
				feriaCritValoracion.getUsuarioCrea()
				});
		}
    }

    // ------------- UpdateOperation -------------------------------------
    class UpdateOperation extends SqlUpdate	{
    	private static final String sql = "UPDATE REG_FERIA_CRIT_VALORACION_T SET "
					  + " ID_CRITERIO = ? ,"
					  + " ID_FERIA_ARTESANAL = ? ,"
					  + " CODIGO = ? ,"
					  + " VALORACION = ? ,"
					  + " OBSERVACION = ? ,"
					  + " ESTADO = ? ,"
					  + " USUARIO_MODIFICA = ? ,"
					  + " FECHA_MODIFICA = SYSDATE "
				  	  + WHERE
			  	  	  + " ID = ? ";
		public  UpdateOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters			
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.INTEGER));
			compile();
		}
		public void execute(FeriaCritValoracion feriaCritValoracion) {
			UpdateOperation.this.update( new Object[]{
				feriaCritValoracion.getIdCriterio(),
				feriaCritValoracion.getIdFeriaArtesanal(),
				feriaCritValoracion.getCodigo(),
				feriaCritValoracion.getValoracion(),
				feriaCritValoracion.getObservacion(),
				feriaCritValoracion.getEstado(),				
				feriaCritValoracion.getUsuarioModifica(),
				feriaCritValoracion.getId(),
				});
		}
    }

    // ------------- DeleteOperation -------------------------------------
    class DeleteOperation extends SqlUpdate	{
		private static final String sql = "DELETE FROM REG_FERIA_CRIT_VALORACION_T "
										+ WHERE
			  	  	  					+ " CODIGO = ? ";
		public DeleteOperation(DataSource ds) {
			super(ds,sql);
			declareParameter( new SqlParameter(Types.CHAR) );
			compile();
		}
		public void execute(FeriaCritValoracion feriaCritValoracion) {
			this.update( new Object[]{
				feriaCritValoracion.getCodigo(),
			});
		}
	}

    // ------------- DeleteByPrimaryKey ----------------------------------
    class DeleteByPrimaryKey extends SqlUpdate	{
		private static final String sql = "DELETE FROM REG_FERIA_CRIT_VALORACION_T "
										+ WHERE
			  	  	  					+ " ID = ? ";
		public DeleteByPrimaryKey(DataSource ds) {
			super(ds,sql);
			declareParameter( new SqlParameter(Types.INTEGER) );
			compile();
		}	
		public void execute(long id) {
			this.update( new Object[]{
					new Long(id)
			});
		}	
    }

    // ---- BaseQueryOperation for Mapping ResultSet values --------------
	abstract class BaseQueryOperation extends MappingSqlQuery {
		public BaseQueryOperation(DataSource ds,String sql) {
	   		super(ds,sql);
		}	
		protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
			FeriaCritValoracion feriaCritValoracion = new FeriaCritValoracion();
				feriaCritValoracion.setId(new Long(rs.getLong("ID")));
				feriaCritValoracion.setIdCriterio(new Long(rs.getLong("ID_CRITERIO")));
				feriaCritValoracion.setIdCriterioStr(rs.getString("ID_CRITERIO"));
				feriaCritValoracion.setIdFeriaArtesanal(new Long(rs.getLong("ID_FERIA_ARTESANAL")));
				feriaCritValoracion.setCodigo(rs.getString("CODIGO"));
				feriaCritValoracion.setValoracion(rs.getString("VALORACION"));
				feriaCritValoracion.setObservacion(rs.getString("OBSERVACION"));
				feriaCritValoracion.setEstado(rs.getString("ESTADO"));
				feriaCritValoracion.setUsuarioCrea(rs.getString("USUARIO_CREA"));
				feriaCritValoracion.setFechaCrea(rs.getDate("FECHA_CREA"));		
				feriaCritValoracion.setUsuarioModifica(rs.getString("USUARIO_MODIFICA"));
				feriaCritValoracion.setFechaModifica(rs.getDate("FECHA_MODIFICA"));		
			return feriaCritValoracion;
		}
    }

    // ------------- FindByPrimaryKeyQuery -------------------------------
    class FindByPrimaryKeyQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE
			  	  	  					+ " ID = ? ";
    	public FindByPrimaryKeyQuery(DataSource ds) {
    		super(ds,sql);
			declareParameter( new SqlParameter(Types.INTEGER) );
			compile();
    	}
    	public FeriaCritValoracion runQuery(long id) {
			List result =  execute( new Object[]{new Long(id)});
			if ( result.size() > 0 ) {
        		return (FeriaCritValoracion) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByCriteriaQuery ---------------------------------    
    class FindByCriteriaQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 "
			  	  	  					+ " AND ID_FERIA_ARTESANAL = ? "
										+ "";
    	public FindByCriteriaQuery(DataSource ds) {
    		super(ds,sql);
			declareParameter( new SqlParameter(Types.INTEGER) );
			compile();
    	}
    	public FeriaCritValoracion runQuery(FeriaCritValoracion feriaCritValoracion) {
			List result =  execute( new Object[]{
					feriaCritValoracion.getIdFeriaArtesanal()
				});
			if ( result.size() > 0 ) {
        		return (FeriaCritValoracion) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindAllQuery ----------------------------------------    
    class FindAllQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 ";
    	public FindAllQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public List runQuery() {
			List result =  execute();
			if ( result.size() > 0 ) {
        		return  result;
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindAllQuery ----------------------------------------    
    class FindFeriaId extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " ID_FERIA_ARTESANAL=? ";
    	public FindFeriaId(DataSource ds) {
    		super(ds,sql);
    		declareParameter( new SqlParameter(Types.INTEGER) );
			compile();
    	}
    	public List runQuery(FeriaCritValoracion feriaCritValoracion) {
			List result =  execute( new Object[]{
				feriaCritValoracion.getIdFeriaArtesanal(),
				});
			if ( result.size() > 0 ) {
        		return  result;
        	} else {
        		return null;
        	}
    	}
    }
    // ------------- cargarCriterios ----------------------------------------    
    class CargarCriterios extends BaseQueryOperation {
		private static final String sql = "SELECT ID_VALOR_TIPO AS IDCRITERIO,VRT_NOMBRE AS VALORACION FROM ADM_VALOR_TIPO_MAESTRA_T"
										 + " WHERE ID_TIPO=9 ";
    	public CargarCriterios(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public List runQuery() {
			List result =  execute();
			if ( result.size() > 0 ) {
        		return  result;
        	} else {
        		return null;
        	}
    	}
    }
    
    // ------------- FindByActiveStateQuery  -----------------------------
    class FindByActiveStateQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + "1=1";
    	public FindByActiveStateQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public FeriaCritValoracion runQuery(FeriaCritValoracion feriaCritValoracion) {
			List result =  execute( new Object[]{
			});
			if ( result.size() > 0 ) {
        		return (FeriaCritValoracion) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

}
